import moment from 'moment';
import alarmStatus from '../alarm.status';

export const getQueryParams = (properties) => {
    const timer = properties.timer?.startTime ? [
        moment(properties.timer.startTime).startOf('day').valueOf(),
        moment(properties.timer.endTime).endOf('day').valueOf(),
    ] : undefined;
    const data = {
        collection: 'alarm_info_realtime',
        permission: true,
        equalCondition: {
            orgCode: properties.orgCode,
            projectId: properties.projectId,
            masterTenantId:
                properties.masterTenantId?.length > 0
                    ? properties.masterTenantId
                    : undefined,
            deviceType: "physical",
            instanceName: properties?.instanceName,
            confirmStatus: 0,
            alarmStatus: 1,
        },
        inCondition: {
            resourceCode:
                properties.resourceCode?.length > 0
                    ? properties.resourceCode
                    : undefined,
            alarmLvl:
                properties.alarmLvl?.length > 0
                    ? properties.alarmLvl
                    : undefined,
            alarmType:
                properties.alarmType?.length > 0
                    ? properties.alarmType
                    : undefined,
            isIntellect:
                (properties.isIntellect?.length === 1 && properties.isIntellect[0] === 1)
                    ? [1]
                    : undefined,
        },
        notEqualCondition: {
            isIntellect:
                (properties.isIntellect?.length === 0 || properties.isIntellect?.length === 2)
                    ? undefined
                    : (properties.isIntellect && properties.isIntellect[0] == 0 ? 1 : undefined),
        },
        rangeCondition: {
            alarmTime: timer,
        },
        likeCondition: {
            alarmDesc: properties.alarmDesc,
        },
        returnFields: {
            _id: '',
            resourceCode: '',
            instanceId: '',
            instanceName: '',
            resourceName: '',
            masterTenantName: '',
            cloud: '',
            cloudName: '',
            brand: '',
            alarmType: '',
            isIntellect: '',
            alarmDesc: '',
            orgName: '',
            projectName: '',
            alarmLvl: '',
            status: '1',
            alarmTime: '',
            alarmDuration: '',
            alarmRuleId: '',
            indicatorCode: '',
            confirmStaff: '',
            confirmStatus: '',
            confirmTime: '',
            alarmStatus: '',
            alarmRecoveryTime: '',
            alarmRecoveryTimeStr: '',
        },
        orderField: {
            alarmStatus: 'desc',
            confirmStatus: 'asc',
            alarmLvl: 'asc',
            alarmTime: 'desc',
        },
    };
    return data
}
export const screenHandle = ({ data, properties, resource }) => {
    // 筛选条件处理
    // 筛选条件
    let screenStr = '';
    // 租户
    if (properties.masterTenantId?.length > 0) {
        screenStr += '，租户：' + data.masterTenantName;
    }
    // 时间范围
    if (properties.timer) {
        screenStr += `开始时间：${moment(properties.timer.startTime).format('YYYY-MM-DD')} ~ 结束时间：${moment(properties.timer.endTime).format('YYYY-MM-DD')}`;
    }
    // 告警名称
    if (properties.instanceName) {
        screenStr += `，告警名称：${properties.instanceName} `;
    }
    // 告警描述
    if (properties.alarmDesc) {
        screenStr += `，告警描述：${properties.alarmDesc} `;
    }
    // 告警对象
    let resourceCode = properties.resourceCode;
    if (resourceCode && resourceCode.length > 0) {
        screenStr += '，告警对象：';
        resourceCode.forEach((item, index) => {
            for (let i = 0; i < resource.length; i++) {
                const resourceItem = resource[i];
                if (resourceItem.value == item) {
                    screenStr += `${resourceItem.text}${index == resourceCode.length - 1 ? ' ' : '、'}`;
                    break;
                }
            }
        });
    }
    // 告警类型
    if (properties.alarmType?.length > 0) {
        screenStr += '，告警类型：' + properties.alarmType;
    }
    // 级别
    let alarmLvl = properties.alarmLvl;
    if (alarmLvl && alarmLvl.length > 0) {
        screenStr += '，级别：';
        alarmLvl.forEach((item, index) => {
            for (let i = 0; i < alarmStatus.infoRankFileter.length; i++) {
                const alarmLvlItem = alarmStatus.infoRankFileter[i];
                if (alarmLvlItem.value == item) {
                    screenStr += `${alarmLvlItem.text}${index == alarmLvl.length - 1 ? ' ' : '、'}`;
                    break;
                }
            }
        });
    }
    // 告警状态
    let status = properties.alarmStatus;
    if (status && status.length > 0) {
        screenStr += '，告警状态：';
        status.forEach((item, index) => {
            for (let i = 0; i < alarmStatus.infoAlarmStatusFilter.length; i++) {
                const statusItem = alarmStatus.infoAlarmStatusFilter[i];
                if (statusItem.value == item) {
                    screenStr += `${statusItem.text}${index == status.length - 1 ? ' ' : '、'}`;
                    break;
                }
            }
        });
    }
    // 确认状态
    let statusConfirm = properties.confirmStatus;
    if (statusConfirm && statusConfirm.length > 0) {
        screenStr += '，告警状态：';
        statusConfirm.forEach((item, index) => {
            for (let i = 0; i < alarmStatus.infoConfirmStatusFileter.length; i++) {
                const statusItem = alarmStatus.infoConfirmStatusFileter[i];
                if (statusItem.value == item) {
                    screenStr += `${statusItem.text}${index == statusConfirm.length - 1 ? ' ' : '、'}`;
                    break;
                }
            }
        });
    }
    screenStr += '；';
    if (screenStr[0] === '，') screenStr = screenStr.substring(1)
    if (screenStr === '；') screenStr = '无；'
    // 统计时间
    let statTimeStr = `${moment().format('YYYY-MM-DD HH:mm:ss')}`;
    return {
        筛选条件: screenStr,
        统计时间: statTimeStr,
    };
};
